package cn.alpha.open.autocode.filter;

import java.util.Iterator;
import java.util.List;

import org.springframework.util.StringUtils;

import cn.alpha.open.autocode.convert.ModelConvertor;
import cn.alpha.open.autocode.mavenplugin.FilterCfg;
import cn.alpha.open.autocode.model.ClassModel;
import cn.alpha.open.autocode.model.TemplateModel;

public class ModelFilter 
{
	
	private ModelConvertor modelConvertor;
	
	private FilterCfg filterCfg;
	
	
	public ModelFilter(ModelConvertor modelConvertor,FilterCfg filterCfg)
	{
		this.modelConvertor=modelConvertor;
		this.filterCfg=filterCfg;
	}
	
	public void filter()
	{
		filterClass(modelConvertor.getControllerModels(),filterCfg.getControllerIncludeRegEx(),filterCfg.getControllerExcludeRegEx());
		filterClass(modelConvertor.getEntityModels(),filterCfg.getEntityIncludeRegEx(),filterCfg.getEntityExcludeRegEx());
		filterClass(modelConvertor.getMappingInterfaceModels(),filterCfg.getMapperIncludeRegEx(),filterCfg.getMapperExcludeRegEx());
		filterClass(modelConvertor.getMappingXmlModels(),filterCfg.getMapperIncludeRegEx(),filterCfg.getMapperExcludeRegEx());
		filterClass(modelConvertor.getSerivceModels(),filterCfg.getServiceImplIncludeRegEx(),filterCfg.getServiceImplExcludeRegEx());
		filterClass(modelConvertor.getSerivceInterfaceModels(),filterCfg.getServiceInterfaceIncludeRegEx(),filterCfg.getServiceInterfaceExcludeRegEx());
	}
	
	private void filterClass(List<? extends TemplateModel> list,String includeRegEx,String excludeRegEx)
	{
		Iterator<? extends TemplateModel> it=list.iterator();
		TemplateModel classModel;
		while(it.hasNext())
		{
			classModel=it.next();
			if(!StringUtils.isEmpty(includeRegEx)&&!classModel.getTableName().matches(includeRegEx))
				it.remove();
			if(!StringUtils.isEmpty(excludeRegEx)&&classModel.getTableName().matches(excludeRegEx))
				it.remove();
		}
	}
}
